package com.ld.poetry.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.poetry.config.AppConfigs;
import com.ld.poetry.dao.ResourceConfigMapper;
import com.ld.poetry.entity.ResourceConfig;
import com.ld.poetry.handle.PoetryRuntimeException;
import com.ld.poetry.service.ResourceConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ResourceConfigServiceImpl extends ServiceImpl<ResourceConfigMapper, ResourceConfig> implements ResourceConfigService {
    @Resource
    private AppConfigs appConfigs;


    @Override
    public void initConfig() {
        log.info("init resource config begin.");

        List<ResourceConfig> configList = this.list();

        ResourceConfig config = configList
                .stream()
                .filter(cfg -> Boolean.TRUE.equals(cfg.getSelect()))
                .findAny()
                .orElse(null);
        if(Objects.isNull(config)){
            throw new PoetryRuntimeException("can not found selected resource config.");
        }
        log.info("selected resource config={}",config);
        appConfigs.setSelectedResourceConfig(config);
        appConfigs.setAllResourceConfig(configList);

        log.info("init resource config end.");
    }
}
